package com.ruoyi.fin.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.fin.service.IFinReportService;

/**
 * 财务报表控制器
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/fin/report")
public class FinReportController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(FinReportController.class);
    
    @Autowired
    private IFinReportService finReportService;
    
    /**
     * 获取利润报表数据
     */
    @RequiresPermissions("fin:report:profit")
    @PostMapping("/getProfitData")
    @ResponseBody
    public AjaxResult getProfitData(
            @RequestParam(value = "startDate", required = false) String startDateStr,
            @RequestParam(value = "endDate", required = false) String endDateStr,
            @RequestParam(value = "compareType", required = false, defaultValue = "month") String compareType)
    {
        try {
            // 解析日期
            Date startDate = null;
            Date endDate = null;
            
            if (startDateStr != null && !startDateStr.isEmpty()) {
                startDate = DateUtils.parseDate(startDateStr);
            }
            
            if (endDateStr != null && !endDateStr.isEmpty()) {
                endDate = DateUtils.parseDate(endDateStr);
            }
            
            // 获取数据
            List<Map<String, Object>> data = finReportService.getProfitReportData(startDate, endDate, compareType);
            return AjaxResult.success(data);
        } catch (Exception e) {
            log.error("获取利润报表数据出错", e);
            return AjaxResult.error("获取数据失败，请联系管理员");
        }
    }
    
    /**
     * 账户余额报表页面
     */
    @RequiresPermissions("fin:report:account")
    @GetMapping("/accountBalance")
    public String accountBalance()
    {
        return "fin/report/accountBalance";
    }
    
    /**
     * 获取账户余额数据
     */
    @RequiresPermissions("fin:report:account")
    @PostMapping("/getAccountBalanceData")
    @ResponseBody
    public AjaxResult getAccountBalanceData()
    {
        try {
            List<Map<String, Object>> data = finReportService.getAccountBalanceData();
            return AjaxResult.success(data);
        } catch (Exception e) {
            log.error("获取账户余额数据出错", e);
            return AjaxResult.error("获取数据失败，请联系管理员");
        }
    }
    
    /**
     * 获取账户余额趋势数据
     */
    @RequiresPermissions("fin:report:account")
    @PostMapping("/getAccountBalanceTrendData")
    @ResponseBody
    public AjaxResult getAccountBalanceTrendData(
            @RequestParam(value = "startDate", required = false) String startDateStr,
            @RequestParam(value = "endDate", required = false) String endDateStr,
            @RequestParam(value = "accountId", required = false) Long accountId)
    {
        try {
            // 解析日期
            Date startDate = null;
            Date endDate = null;
            
            if (startDateStr != null && !startDateStr.isEmpty()) {
                startDate = DateUtils.parseDate(startDateStr);
            }
            
            if (endDateStr != null && !endDateStr.isEmpty()) {
                endDate = DateUtils.parseDate(endDateStr);
            }
            
            // 获取数据
            List<Map<String, Object>> data = finReportService.getAccountBalanceTrendData(startDate, endDate, accountId);
            return AjaxResult.success(data);
        } catch (Exception e) {
            log.error("获取账户余额趋势数据出错", e);
            return AjaxResult.error("获取数据失败，请联系管理员");
        }
    }
} 